
/**
 * Write a description of class MyQueueLink here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
package jccflux;
public class MyQueueLink implements MyQueue
{
    private Node head;
    private Node tail;
    
    public boolean isEmpty() { return head == null; }
    public boolean isFull()  { return false; }
    public boolean isElement(Object item)
    {
        boolean found = false;
        
        if (!isEmpty())
        {
            Node node = head;
            
            while (node != null)
            {
                if (node.getItem().equals(item))
                {
                    found = true;
                    break;
                }
                
                node = node.getNext();
            }
        }
        
        return found;
    }
    
    public boolean enQueue(Object item)
    {
        boolean ok = false;
        
        if(!isElement(item))
        {
            Node node = new Node(item);
            
            if(!isEmpty())
            {
                tail.setNext(node);
                tail = node;
            }
            else
            {
                head = node;
                tail = node;
            }            
        }
        
        return ok;
    }
    
    public Object deQueue()
    {
        Object item = null;
        
        if(!isEmpty())
        {
            Node node = head;
            
            item = node.getItem();
            head = node.getNext();
            node = null;
        }
        
        return item;
    }
    
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        
        Node node = head;
        
        while( node != null)
        {
            sb.append(node.getItem() + " ");
            
            node = node.getNext();
        }
        
        return sb.toString();
    }
    
    public static void main(String[] args)
    {
        MyQueueLink link = new MyQueueLink();
        
        
        link.enQueue("1");
        link.enQueue("2");
        link.enQueue("3");
        link.enQueue("4");
        link.enQueue("5");
        link.enQueue("6");
        link.enQueue("7");
        link.enQueue("8");
        link.enQueue("9");
        link.enQueue("0");
        link.enQueue("11");
        link.enQueue("12");
        link.enQueue("13");
        link.enQueue("14");
        link.enQueue("15");
        
        System.out.println(link);
        
        
        link.deQueue();
        link.deQueue();
        link.deQueue();
        link.deQueue();
        link.deQueue();
        
        
        System.out.println(link);
        
        
        
    }
}
